//
//  UIGridView.m
//  iphoneMobo
//
//  Created by Mac Book-1 on 10-7-19.
//  Copyright 2010 shijiazheng. All rights reserved.
//

#import "UIGridView.h"

@implementation UIGridView

@synthesize iCol;
@synthesize iRow;

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    if ((self = [super initWithFrame:frame style:style])) {
		[self setSeparatorStyle:UITableViewCellSeparatorStyleNone];
		//self.iType = KGridView;
		self.iCol = 0;
		self.iRow = 0;
		iItemHeight = 50;
    }
	NSLog(@"new a grid ==================");
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void) layoutGridItems
{
	CGRect rect = self.bounds;
	int width = rect.size.width;
	int count = self.iItemsArray.count;
	if (count > 0) {
		GridItem* item = [self.iItemsArray objectAtIndex:0];
		CGSize imgSize;
		int itemWidth = 0;
		int imgHeight = 0;
		if (item.iNomalImg) {
			imgSize = [[CImageFactory getInstance] getImageSize:item.iNomalImg];
			itemWidth = imgSize.width;
			imgHeight = imgSize.height;
		}
		if (itemWidth < 80) {
			itemWidth = 80;
		}
		iCol = width / itemWidth;
		if (iCol != 0) {
			itemWidth += (width - iCol * itemWidth) / iCol;
		}
		else {
			iCol = 1;
			itemWidth = width;
		}
		iItemHeight = imgHeight + 18 + 18;//字体高度 间隔
		if (iItemHeight < 80) {
			iItemHeight = 80;
		}
		
		iRow = count / iCol;
		if (count % iCol)
		{
			iRow++;
		}
		int x = 0;
		int y = 0;
		for (int i = 0; i < iRow; i++) 
		{
			for (int j = 0; j < iCol; j++) {
				if (i * iCol + j >= count) {
					return ;
				}
				item = [self.iItemsArray objectAtIndex:i * iCol + j];
				rect = CGRectMake(x, y, itemWidth, iItemHeight);
				item.frame = rect;
				x += itemWidth;
			}
			x = 0;
		}
	}
}

-(int) getItmeHeight
{
	return iItemHeight;
}

- (void)dealloc {
	NSLog(@"dealloc a grid ==================");
    [super dealloc];
}

@end
